import esriLoader from 'esri-loader'
 var activeMap = null,_activeMapEventHandlers=[];
  var _bindActiveMapEvents = function (maps) {
    var self = this;
    //放大联动
    _activeMapEventHandlers.push(activeMap.on("zoom-end", function (evt) {
      maps.forEach(function (map) {
        if (map != activeMap) {
          map.setExtent(evt.extent);
        }
          });
      }));
    //平移联动
    _activeMapEventHandlers.push(activeMap.on("pan-end", function (evt) {
      maps.forEach(function (map) {
        if (map != activeMap) {
          map.setExtent(evt.extent);
          }
          });
      }));


  },
  _clearActiveMapEvents= function () {
   _activeMapEventHandlers.forEach(function (eventHandler) {
      eventHandler.remove();
    });
    _activeMapEventHandlers = [];
  },
  initExtentEvent =  function (mapIds) {
  var self = this;
    esriLoader.loadModules(["widget/MapManager"])
      .then(([MapManager]) => {
        var otherMaps = [];
        for (var i=0;i<mapIds.length;i++){
          var map = MapManager.getMap(mapIds[i]);
          //debugger;
           
          if(map){
            if(mapIds[i]!="mainMap"){
              map.setExtent(MapManager.getMap(mapIds[0]).extent);
             }
            otherMaps.push(map);
            map.on("mouse-over",function (evt) {
              activeMap = this;
              self._clearActiveMapEvents();
              self._bindActiveMapEvents(otherMaps);
            });
          }

        }
    })
      .catch(err => {
        // handle any script or module loading errors
        console.error(err);
      })
  }
export default {
  _bindActiveMapEvents,_clearActiveMapEvents,initExtentEvent
}
